/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */


/**
 * RPC wrapper class for source/build abstraction and handies
 */
qx.Class.define("webapp.network.Rpc", {

  extend : qx.io.remote.Rpc,

  construct : function(serviceName)
  {
	  if(!serviceName)
    {
      throw new Error("serviceName is not specified");
    }

    this.base(arguments);

    this.set({
      url         : qx.core.Environment.get("webapp.serviceUrl"),
      crossDomain : qx.core.Environment.get("webapp.crossDomain"),
      timeout     : 30 * 1000,
      serviceName : serviceName
    });
    
    this.addListener("failed", this._onFailed, this);
  },

  members : {

    _onFailed : function(response)
    {
      dialog.Dialog.error(qx.locale.Manager.tr("Request failed") + "\n" + response.getData());
    },

    // override
    addListener : function(type, func, obj)
    {
      if(type == "failed")
      {
        this.removeListener("failed", this._onFailed, this);
      }

      return this.base(arguments, type, func, obj);
    },

    /**
     * @param mathod {String}
     * @param varargs {var}
     */
    call : function(method)
    {
      var args = Array.prototype.slice.call(arguments);
      args.unshift(true);
      args = args.filter(function(item)
      {
        return item !== undefined;
      });
      
      this.callAsyncListeners.apply(this, args);
    },

    getCallUrl : function(method, params)
    {
      var result = this.getUrl();

      var uniqueId = Date.now();

      var idKey  = qx.io.remote.transport.Script.ScriptTransport_ID_PARAM; 
      result    += (result.indexOf("?") >= 0 ? "&" : "?") + idKey + "=" + uniqueId;

      var requestObject = {
        "service" : this.getServiceName(),
        "method"  : method,
        "id"      : uniqueId,
        "params"  : params ? params : []
      };
      if(this.getServerData())
      {
        requestObject["server_data"] = this.getServerData(); 
      }

      var data = qx.lang.Json.stringify(requestObject);
      if(data != null)
      {
        var dataKey  = qx.io.remote.transport.Script.ScriptTransport_DATA_PARAM; 
        result      += "&" + dataKey + "=" + encodeURIComponent(data);
      }

      return result;
    },

    getCallUrlParameters : function(method, params)
    {
      var result        = {};
      var uniqueId      = Date.now();
      var requestObject = {
        "service" : this.getServiceName(),
        "method"  : method,
        "id"      : uniqueId,
        "params"  : params ? params : []
      };
      if(this.getServerData())
      {
        requestObject["server_data"] = this.getServerData(); 
      }      
      
      var transport = qx.io.remote.transport.Script;
      
      result[transport.ScriptTransport_ID_PARAM]   = uniqueId;
      result[transport.ScriptTransport_DATA_PARAM] = qx.lang.Json.stringify(requestObject);

      return result;
    }

  }

});